package com.lingxu.module.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.util.UUID32Generator;
import com.lingxu.module.info.entity.CommentVO;
import com.lingxu.module.info.entity.InfoReleaseComment;
import com.lingxu.module.info.mapper.InfoReleaseCommentMapper;
import com.lingxu.module.info.service.InfoReleaseCommentService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Title: InfoReleaseCommentServiceImpl
 * @Package: com.lingxu.module.info.service.impl
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2023/8/10 15:07
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class InfoReleaseCommentServiceImpl extends ServiceImpl<InfoReleaseCommentMapper,InfoReleaseComment>
        implements InfoReleaseCommentService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InfoReleaseCommentMapper infoReleaseCommentMapper;


    @Override
    public Result addComment(InfoReleaseComment comment) {
        try {
            comment.setCreateTime(new Date());
            //如果id不为空说明不是首条评论
            if(!StringUtils.isEmpty(comment.getId())){
                String pid = comment.getId();
                comment.setId(UUID32Generator.generate());
                comment.setPid(pid);
            }else {
                //重新赋值id
                comment.setId(UUID32Generator.generate());
                comment.setPid("0");
                infoReleaseCommentMapper.insert(comment);
            }
            return Result.ok("操作成功");
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.error("操作异常");
        }
    }


    @Override
    public Result findCommentList(String infoId) {
        try {
            List<CommentVO> data = infoReleaseCommentMapper.findCommentList("0",infoId);
            //data.forEach(this::setChildren);
            return Result.ok(data,"操作成功");
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.error("操作异常");
        }
    }


    private void setChildren(CommentVO commentVO){
        List<CommentVO> children = baseMapper.findCommentList(commentVO.getId(),commentVO.getInfoId());
        if (!children.isEmpty()) {
            commentVO.setCommentVOList(children);
            children.forEach(this::setChildren);
        }
    }
}
